﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

public partial class Pages_Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetContent();
    }




    /// <summary>
    /// 
    /// </summary>
    private void GetContent()
    {
        var news = new Business.News { NewsID = 3 }.NewsGetByNewsId();
        if (news == null) return;
        ltrTitle.Text = news.Title;
        ltrSummary.Text = news.Summary;
        ltrContent.Text = news.Content;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SendMailByObject(txtEmail.Text, contact_text.Value, txtName.Text, txtsubject.Text);
    }



    private bool SendMailByObject(string objEmail, string body, string fullName, string subject)
    {
        try
        {
            var settig = (Business.Setting)Session["Setting"];
            var from = new MailAddress(settig.Email);

            var smtpSend = new SmtpClient(settig.SMTP, settig.Port);

            using (var emailMessage = new MailMessage())
            {
                emailMessage.Body = body;
                emailMessage.To.Add(fullName + "<" + objEmail + ">");
                emailMessage.From = from;
                emailMessage.Subject = subject;
                emailMessage.BodyEncoding = Encoding.UTF8;
                smtpSend.Credentials = new NetworkCredential(settig.Email, settig.Password);
                smtpSend.UseDefaultCredentials = false;
                smtpSend.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                smtpSend.Send(emailMessage);
                return true;
            }
        }
        catch
        {
            return false;
        }

    }
}